/*
 * MIT License
 * 
 * Copyright (c) 2024-2025 milkpotatoes
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef SRC_UTILS_INCLUDE_LOG_H
#define SRC_UTILS_INCLUDE_LOG_H

#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>

constexpr const std::string_view DEFAULT_LOG_TAG{"default"};

enum class RunnerLogLevel : uint8_t
{
    DEBUG,
    INFO,
    WARNING,
    ERROR,
    FATAL,
};

class RunnerLogger
{
public:
    explicit RunnerLogger(const std::string_view &tag, const RunnerLogLevel level = RunnerLogLevel::DEBUG);
    // clang-format off
    explicit RunnerLogger(const std::string &tag, const RunnerLogLevel level = RunnerLogLevel::DEBUG)
        : RunnerLogger(DEFAULT_LOG_TAG, level) {}
    explicit RunnerLogger(const char *tag, const RunnerLogLevel level = RunnerLogLevel::DEBUG)
        : RunnerLogger(std::string_view(tag), level) {}
    explicit RunnerLogger(const RunnerLogLevel level = RunnerLogLevel::DEBUG) : RunnerLogger(DEFAULT_LOG_TAG, level) {}
    // clang-format on

    ~RunnerLogger();

    template <typename T> RunnerLogger &operator<<(const T &val)
    {
        stream_ << val;
        return *this;
    }

    static void SetBaseLogLevel(RunnerLogLevel level);

private:
    RunnerLogger(const RunnerLogger &C) = delete;
    RunnerLogger(const RunnerLogger &&C) = delete;

    const RunnerLogLevel level_{RunnerLogLevel::DEBUG};
    std::ostringstream stream_{};

    static RunnerLogLevel baseLevel_;
};

#define __FILENAME__ strrchr(__FILE__, '/') + 1

#define RUNNER_LOG(tag, level) \
    RunnerLogger(tag, RunnerLogLevel::level) << "[" << __FUNCTION__ << " " << __FILENAME__ << ":" << __LINE__ << "] "

#define DEFAULT_LOGGER(level) \
    RunnerLogger(RunnerLogLevel::level) << "[" << __FUNCTION__ << " " << __FILENAME__ << ":" << __LINE__ << "] "

inline static void LogError(std::string str)
{
    std::cerr << str << std::endl;
    return;
};

inline static void LogInfo(std::string str)
{
    std::cout << str << std::endl;
    return;
};

#endif /* SRC_UTILS_INCLUDE_LOG_H */
